using MediatR;
using UniversalAdmin.Domain.DomainEvents;

namespace UniversalAdmin.Domain.Entities.App
{
  public class AppRole : EntityBase
  {
    public string RoleName { get; set; } = null!; // 角色名称 (唯一)
    public string? Description { get; set; } // 角色描述 (可空)


    public ICollection<AppUser> Users { get; set; } = new List<AppUser>();
    public ICollection<AppPermission> Permissions { get; set; } = new List<AppPermission>();
    public ICollection<AppMenu> Menus { get; set; } = new List<AppMenu>();

    public void AssignPermission(AppPermission permission)
    {
      ArgumentNullException.ThrowIfNull(permission);

      if (Permissions.Any(p => p.Id == permission.Id))
      {
        // 已经有该权限
        return;
      }
      Permissions.Add(permission);
    }

    // 领域事件发布
    public void OnCreated() => AddDomainEvent(new RoleCreatedEvent(this));
    public void OnUpdated() => AddDomainEvent(new RoleUpdatedEvent(this));
    public void OnDeleted() => AddDomainEvent(new RoleDeletedEvent(this));
  }
}
